package com.itheima.service.Impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private RoleDao roleDao;

    /**
     * 根据username查询权限菜单
     *
     * @param username
     * @return
     */
    @Override
    public List<Menu> findMenuByUsername(String username) {
        //获取权限Id
        List<Role> roleIds = roleDao.findByUserId(userDao.findByUsername(username).getId());
        //存放所有菜单的集合
        List<Menu> allMenu = new ArrayList<>();
        //存放一级菜单集合
        List<Menu> Menus = new ArrayList<>();
        roleIds.forEach(roleId -> {
            //根据权限获取所有的菜单
            List<Menu> menus = menuDao.findMenuByRoleId(roleId.getId());
            for (Menu menu : menus) {
                allMenu.add(menu);
                //将一级目录存入集合
                if (menu.getParentMenuId() == null) {
                    Menus.add(menu);
                }
            }
        });
        for (Menu menu : Menus) {
            findChildren(menu, allMenu);
        }

        return Menus;
    }

    /**
     * 添加菜单
     *
     * @param menu
     */
    @Override
    public void add(Menu menu) {
        //一级菜单
        //设置路由路径
        menu.setPath(Integer.toString(menuDao.findMaxPath() + 1));
        //设置菜单级别
        menu.setLevel(1);
        menuDao.add(menu);
        //给管理员授权
        menuDao.addRole(menu.getId());
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<Role> findAllRole() {
        return menuDao.findAllRole();
    }

    @Override
    public void addSon(Menu menu, Integer[] roleIds) {
        Menu parentMenu = menuDao.findMenuById(menu.getParentMenuId());
        //获取上级菜单内容
        Integer level = parentMenu.getLevel();
        String path = parentMenu.getPath();
        //设置菜单级别
        menu.setLevel(level + 1);
        //设置path路由路径
        if (menu.getLevel() == 2) {
            //新添加的菜单为二级菜单拼接 /
            path = "/" + path + "-" + menu.getPriority();
        } else {
            path = path + "-" + menu.getPriority();
        }
        menu.setPath(path);
        menuDao.add(menu);
        //授权
        if (roleIds != null) {
            for (Integer roleId : roleIds) {
                menuDao.addByRoles(menu.getId(), roleId);
            }
        }
    }

    /**
     * 根据menuId获取菜单信息
     *
     * @param id
     * @return
     */
    @Override
    public Menu findMenuById(Integer id) {
        return menuDao.findMenuById(id);
    }

    @Override
    public Integer[] findRole(Integer menuId) {
        return menuDao.findRole(menuId);
    }

    /**
     * 修改菜单和关联的角色
     *
     * @param menu
     * @param roleIds
     */
    @Override
    public void update(Menu menu, Integer[] roleIds) {
        //修改菜单信息
        menuDao.update(menu);
        //删除原有关联关系
        menuDao.deleteMenuAndRole(menu.getId());
        //重新建立关系
        if (roleIds != null && roleIds.length > 0)
            for (Integer roleId : roleIds) {
                menuDao.addByRoles(menu.getId(), roleId);
            }
    }

    /**
     * 删除菜单
     *
     * @param menuId
     */
    @Override
    public void delete(Integer menuId) {
        Menu menu = menuDao.findMenuById(menuId);
        List<Menu> children = menuDao.findChildren(menuId);
        //判断是否有子菜单
        if (children != null && children.size() > 0) {
            for (Menu child : children) {
                //删除子菜单角色关系
                menuDao.deleteMenuAndRole(child.getId());
                //删除子菜单
                menuDao.delete(child.getId());
            }
        }
        //删除菜单角色关系
        menuDao.deleteMenuAndRole(menuId);
        //删除菜单
        menuDao.delete(menuId);
    }


    //无级分类查询子菜单方法
    private void findChildren(Menu parentMenu, List<Menu> allMenu) {
        List<Menu> children = new ArrayList<>();
        for (Menu menu : allMenu) {
            if (parentMenu.getId() == menu.getParentMenuId()) {
                children.add(menu);
                parentMenu.setChildren(children);
                findChildren(menu, allMenu);
            }
        }
    }
}
